package com.example.wannerone.myapplication123;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.wannerone.http.GetImage;
import com.example.wannerone.http.HttpPostUtils;

import java.util.HashMap;
import java.util.Map;

public class MonitorActivity extends AppCompatActivity {
    private String result;
    //刷新按钮
    Button btn;
    private Bitmap bitmap1;

    String u_id=null;
    String i_id=null;
    Bitmap bitmap;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitor);
        btn=(Button) findViewById(R.id.btn);
        imageView=(ImageView) findViewById(R.id.imageview);


        final MyApplication application = (MyApplication) getApplication();
        System.out.println("application.getU_id=" + application.getU_id());

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                 u_id=String.valueOf(application.getU_id());
                final Map<String, Object> userInfo = new HashMap<String, Object>();
                userInfo.put("u_id", u_id);
                String jsonString = JSONObject.toJSONString(userInfo);
                final String info = "info=" + jsonString;
                if(u_id!=null){
                    new Thread() {
                        public void run() {
                            String response = HttpPostUtils.doPostRequest("/monitor/getImage", info);
                            JSONObject resultJson = JSON.parseObject(response);
                            String finalResult = resultJson.getString("result");
                            System.out.println("0-0-0-0-0-0-");
                            if (finalResult.equals("ok")){
                                i_id = resultJson.getString("i_id");
                                System.out.println("------------"+i_id);
                                final Map<String, Object> userInfo1 = new HashMap<String, Object>();
                                userInfo1.put("i_id", i_id);
                                String jsonString = JSONObject.toJSONString(userInfo1);
                                final String info = "info=" + jsonString;
                                System.out.println("+++++info="+info);
                                do{
                                    try {
                                        Thread.sleep(2000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    System.out.println("=-=-=-=-=-=-==");
                                    response=HttpPostUtils.doPostRequest("/monitor/havaImage", info);
                                    resultJson = JSON.parseObject(response);
                                    finalResult = resultJson.getString("result");
                                }while (!finalResult.equals("ok"));
                                if(finalResult.equals("ok")){
                                    String directoryOfLockId = resultJson.getString("directoryOfLockId");
                                    String filename = resultJson.getString("filename");
                                    System.out.println("获取图片路径成功");
                                    String url=
                                            "http://10.0.2.2:8080/imagesA/"+directoryOfLockId+"/"+filename;
                                    bitmap=new GetImage().getHttpBitmap(url);
                                }
                            }else{
                                System.out.println("错误");
                            }
                            Message message = Message.obtain();
                            message.obj = bitmap;
                            mHanlder.sendMessage(message);
                        }
                    }.start();
                }
//                //建立一个意图,参数为（当前的Activity类对象，需要开的的Activity类）
//                Intent intent = new Intent(Main2Activity.this, makeuserActivity.class);
//                //启动意图
//                startActivity(intent);
            }
        });
    }

    private Handler mHanlder = new Handler() {
        @Override
        public void handleMessage(Message message) {
            bitmap1 = (Bitmap) message.obj;
            imageView.setImageBitmap(bitmap1);
        }
    };

}
